/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


#include <QTextCodec>
#include "hunspell_spellchecker.h"
#include "log.h"

namespace Wishmaster
{
	HunspellSpellChecker::HunspellSpellChecker() : SpellChecker(),
		m_hunspell(0)
	{
	}

	HunspellSpellChecker::~HunspellSpellChecker()
	{
		if(m_hunspell)
			delete m_hunspell;
	}

	bool HunspellSpellChecker::addDictionary(const QString& filename)
	{
		if(!m_hunspell)
		{
			m_hunspell = new Hunspell((filename + ".aff").toLocal8Bit().data(), (filename + ".dic").toLocal8Bit().data());
			return true;
		}
		return !(m_hunspell->add_dic((filename + ".dic").toLocal8Bit().data()) == 0);
	}

	bool HunspellSpellChecker::spell(const QString& word)
	{
		QTextCodec* codec = QTextCodec::codecForName(m_hunspell->get_dic_encoding());
		int hret = m_hunspell->spell(codec->fromUnicode(word).data()); // toUtf8() ?
		return hret != 0;
	}

	QStringList HunspellSpellChecker::suggest(const QString& word)
	{
		// TODO
		return QStringList();
	}

	bool HunspellSpellChecker::addWord(const QString& word)
	{
		int hret = m_hunspell->add(word.toLocal8Bit().data());
		return hret != 0;
	}

	QString HunspellSpellChecker::id()
	{
		return "hunspell";
	}
}

